package com.easylinkin.linkappapi.lobar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.dto.UserBlacklistRecordDTO;
import com.easylinkin.linkappapi.lobar.entity.UserBlacklistRecord;
import com.easylinkin.linkappapi.lobar.mapper.UserBlacklistRecordMapper;
import com.easylinkin.linkappapi.lobar.service.UserBlacklistRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import java.util.Optional;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 黑名单操作记录表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class UserBlacklistRecordServiceImpl extends ServiceImpl<UserBlacklistRecordMapper, UserBlacklistRecord> implements UserBlacklistRecordService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param userBlacklistRecord
   */
  @Override
  public void insert(UserBlacklistRecord userBlacklistRecord) {
    /**
     * 验证重复
     */
//    this.checkExist(userBlacklistRecord);
    //设置基本属性
    this.setBase(userBlacklistRecord);
    this.save(userBlacklistRecord);
  }

  /**
   * 根据id编辑
   * @param userBlacklistRecord
   */
  @Override
  public void updateOne(UserBlacklistRecord userBlacklistRecord) {
    /**
     * 验证重复
     */
    this.checkExist(userBlacklistRecord);
    //设置基本属性
    this.setBase(userBlacklistRecord);
    this.updateById(userBlacklistRecord);
  }

  @Override
  public IPage<UserBlacklistRecordDTO> queryListByPage(RequestModel<UserBlacklistRecordDTO> requestModel) {
    Page page = requestModel.getPage();
    UserBlacklistRecordDTO blacklistRecordDTO = requestModel.getCustomQueryParams();
    //项目id 过滤
    if (null != linkappUserContextProducer.getCurrent() && !Boolean.TRUE.equals(requestModel.getCustomQueryParams().getIsAll())){
      blacklistRecordDTO.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    }
    IPage<UserBlacklistRecordDTO> userBlacklistRecordIPage = baseMapper.queryListByPage(page, blacklistRecordDTO);
    return userBlacklistRecordIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(UserBlacklistRecord userBlacklistRecord) {
    QueryWrapper<UserBlacklistRecord> queryWrapper = new QueryWrapper<>();
    //todo 设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(userBlacklistRecord.getId()).ifPresent(id -> queryWrapper.ne("id_",userBlacklistRecord.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该黑名单操作记录已存在");
    }
  }

  /**
   * 设置基本属性
   * @param userBlacklistRecord
   */
  private void setBase(UserBlacklistRecord userBlacklistRecord) {
    userBlacklistRecord.setModifyTime(new Date());
    if (null != linkappUserContextProducer.getCurrent()){
      userBlacklistRecord.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == userBlacklistRecord.getId()){
      if (null != linkappUserContextProducer.getCurrent()){
        userBlacklistRecord.setCreatorId(linkappUserContextProducer.getCurrent().getId());
        if (StringUtils.isBlank(userBlacklistRecord.getTenantId())){
          userBlacklistRecord.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        }
      }
      userBlacklistRecord.setCreateTime(new Date());
    }
  }
}
